package com.jianguo.dao;

import com.jianguo.bean.Article;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author ly
 * @date 2021/3/5
 * @email 664162337@qq.com
 * @wechat ly19870316
 */
@Repository
public class ArticleJDBCDAO {
    // @Resource
    // private JdbcTemplate jdbcTemplate;

    @Resource
    private JdbcTemplate primaryJdbcTemplate;

    @Resource
    private JdbcTemplate secondaryJdbcTemplate;

    //保存文章
    public int save(Article article) {
        //jdbcTemplate.update适合于insert 、update和delete操作；
        // int update = jdbcTemplate.update("INSERT INTO t_021_article(author, title,content,create_time) values(?, ?, ?, ?)",
        //         article.getAuthor() + "-jdbcTemplate",
        //         article.getTitle(),
        //         article.getContent(),
        //         article.getCreateTime());
        int update1 = primaryJdbcTemplate.update("INSERT INTO t_021_article(author, title,content,create_time) values(?, ?, ?, ?)",
                article.getAuthor() + "-primaryJdbcTemplate",
                article.getTitle(),
                article.getContent(),
                article.getCreateTime());
        int update2 = secondaryJdbcTemplate.update("INSERT INTO t_021_article(author, title,content,create_time) values(?, ?, ?, ?)",
                article.getAuthor() + "-secondaryJdbcTemplate",
                article.getTitle(),
                article.getContent(),
                article.getCreateTime());
        return update1 + update2;
    }

    //删除文章
    // public void deleteById(Long id) {
    //     //jdbcTemplate.update适合于insert 、update和delete操作；
    //     jdbcTemplate.update("DELETE FROM t_021_article WHERE id = ?", id);
    //
    // }
    //
    // //更新文章
    // public void updateById(Article article) {
    //     //jdbcTemplate.update适合于insert 、update和delete操作；
    //     jdbcTemplate.update("UPDATE t_021_article SET author = ?, title = ? ,content = ?,create_time = ? WHERE id = ?",
    //             article.getAuthor(),
    //             article.getTitle(),
    //             article.getContent(),
    //             article.getCreateTime(),
    //             article.getId());
    //
    // }
    //
    // //根据id查找文章
    // public Article findById(Long id) {
    //     //queryForObject用于查询单条记录返回结果
    //     return (Article) jdbcTemplate.queryForObject("SELECT * FROM t_021_article WHERE id=?",
    //             new Object[]{id}, new BeanPropertyRowMapper<>(Article.class));
    // }
    //
    // //查询所有
    // public List<Article> findAll() {
    //     //query用于查询结果列表
    //     return (List<Article>) jdbcTemplate.query("SELECT * FROM t_021_article ", new BeanPropertyRowMapper<>(Article.class));
    // }
}
